#include "include/AVPacketQueue.h"

AVPacketQueue::AVPacketQueue() {
    this->pAVPacketQueue = new std::queue<AVPacket *>();
    pthread_mutex_init(&avPacketMutex, nullptr);
    pthread_cond_init(&avPacketCond, nullptr);
}

AVPacketQueue::~AVPacketQueue() {
    if (pAVPacketQueue != nullptr) {
        clear();
        delete (pAVPacketQueue);
        pAVPacketQueue = nullptr;
    }
    pthread_mutex_destroy(&avPacketMutex);
    pthread_cond_destroy(&avPacketCond);
}

void AVPacketQueue::push(AVPacket *avPacket) {
    pthread_mutex_lock(&avPacketMutex);
    pAVPacketQueue->push(avPacket);
    pthread_cond_signal(&avPacketCond);
    pthread_mutex_unlock(&avPacketMutex);
}

AVPacket *AVPacketQueue::pop() {
    AVPacket *avPacket;
    pthread_mutex_lock(&avPacketMutex);
    while (pAVPacketQueue->empty()) {
        pthread_cond_wait(&avPacketCond, &avPacketMutex);
    }
    avPacket = pAVPacketQueue->front();
    pAVPacketQueue->pop();
    pthread_mutex_unlock(&avPacketMutex);
    return avPacket;
}

void AVPacketQueue::clear() {
    // 需要清除队列，还需要清除每个 AVPacket* 的内存数据
}
